package com.hyperfix.washer.listener;

import com.hyperfix.models.washer.pojos.ParamsData;
import com.hyperfix.models.washer.pojos.Status;
import com.hyperfix.models.washer.pojos.WSData;
import com.hyperfix.utils.common.TimeUtil;
import com.hyperfix.washer.common.ParamValidator;
import com.hyperfix.washer.service.RecordService;
import com.hyperfix.washer.websocket.MessageSender;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * @author JiangChunYu
 * 2024/4/19
 */
@Component
public class MessageTransfer {
    @Resource
    private MessageSender messageSender;
    @Resource
    private RecordService recordService;
    @Resource
    private ParamValidator paramValidator;

    public void transmitMessage(byte[] data) {
        ParamsData paramsData = new ParamsData(data); // 解析数据
        LocalDateTime now = LocalDateTime.now();
        String time = TimeUtil.format(now);
        recordService.asyncSave(now, paramsData); // 保存监控记录
        paramValidator.createStatus(paramsData); // 校验各个参数是否未超出上下限
        messageSender.sendStatus(new WSData("status", time, new Status(paramsData.getPipelineId(), paramsData.getStatus())));
        messageSender.sendCurrentUsers(paramsData.getPipelineId(), new WSData("instant", time, paramsData));
    }
}
